# -*- coding: utf-8 -*-

import time

from PageClass.boe_DailyExpenseClass.Class_DailyExpenseBoe import Class_DailyExpenseBoe
from TestCases.common import LoginDepend
from TestCases.common._baseClass import _BaseClass
from Util import config, logger, record


class BoeSubmit(_BaseClass):

    def __init__(self, isExit=True):
        self.isExit = isExit
        self.login = LoginDepend('ecsHost', 'user')
        self.DailyExpense = Class_DailyExpenseBoe(self.login.driver)

    def __exit__(self, *args):
        if super().__exit__(*args):
            if self.isExit:
                self.exit()
        else:
            self.exit()

    def exit(self):
        self.DailyExpense.driver.quit()

    def submit(self, boe_data):
        # 打开费报
        logger.info(f"费报地址为：{config.urlDict.url.ecsHost}")  # .\Config\test\UrlConfig.yml
        # 打开日常费用报账单
        self.DailyExpense.open_boe(boe_data.areaType, boe_data.boeType, boe_data.operationType)

        # 获取单号
        boeNo = self.DailyExpense.getBoeNum()
        record.writeDataToRecord({'boeNo': boeNo})

        self.DailyExpense.input_operationType(boe_data.operationType)
        self.DailyExpense.inputboeAbstract(boe_data.boeAbstract)

        for invoiceData in boe_data.invoiceData:
            # 选择其他纸质票新增
            logger.info(f"发票类型：{invoiceData.invoiceType}，发票参数：{invoiceData}")
            self.DailyExpense.newInvoice(invoiceData)

        # 为发票选择费用分类
        time.sleep(1)
        # 点击
        self.DailyExpense.FeeNameClick()
        # 弹框搜索、选择  # mini弹框
        self.DailyExpense.selectValueFromMiniTable(**{'业务小类编码': boe_data.feeCode, '业务小类名称': boe_data.feeName})

        # 费用归属区
        self.DailyExpense.inputProject(boe_data.project)

        # 保存单据
        # with allure.step("保存单据"):
        #     self.DailyExpense.click_boeSaveButton()
        #     time.sleep(5)
        # 提交单据
        message = self.DailyExpense.click_boeSubmitButton()
        return message
